package com.zz.ecommerce.togatherDeclareMgr.custDeclarationGenerate;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.bean.Result;
import com.zz.ecommerce.common.bean.jnc.db.declForm.TDeclareFormHeadJn;
import com.zz.ecommerce.common.bean.jnc.db.declForm.TDeclareFormItemJn;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.togatherDeclareMgr.custDeclarationGenerate.dao.TDeclareFormHeadJnDao;
import com.zz.ecommerce.togatherDeclareMgr.custDeclarationGenerate.dao.TDeclareFormItemJnDao;
import com.zz.ecommerce.togatherDeclareMgr.custDeclarationGenerate.service.CustDeclarationGenerateService;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 报关单生成
 * Created by admin on 2016/1/14.
 */
@Controller
@RequestMapping("/togatherDeclareMgr/custDeclarationGenerate")
public class CustDeclarationGenerateController
{
    private static final Logger LOG = LoggerFactory.getLogger(CustDeclarationGenerateController.class);

    @Autowired
    private CustDeclarationGenerateService custDeclarationGenerateService;
    @Autowired
    private TDeclareFormHeadJnDao declareFormHeadJnDao;
    @Autowired
    private TDeclareFormItemJnDao declareFormItemJnDao;

    @RequestMapping("/init")
    public String init(ModelMap modelMap)
    {
        Date declareDateStart = DateUtils.truncate(DateUtils.addMonths(new Date(), -1), Calendar.MONTH);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        modelMap.put("declareDateStart", format.format(declareDateStart));

        Date declareDateEnd = DateUtils.addMilliseconds(DateUtils.truncate(new Date(), Calendar.MONTH), -1);
        modelMap.put("declareDateEnd", format.format(declareDateEnd));

        return "/togatherDeclareMgr/custDeclarationGenerate/index";
    }

    /**
     * 查询申报单列表
     *
     * @param head
     * @param request
     * @return
     */
    @RequestMapping("/queryDeclareList")
    @ResponseBody
    public GridResult queryDeclareList(TDeclareFormHeadJn head, GridRequest request) {

        //跨境
        head.setIscross("1");
        //出口
        head.setImSign("E");
        //放行
        head.setStatus("CS6");

        //没生成过报关单的申报单
        int count = declareFormHeadJnDao.countWhere(TableConstants.T_DECLARE_FORM_HEAD_JN, head, "AND RELATE_DECL_CUST_ID IS NULL");
        List<TDeclareFormHeadJn> orderHeadList = declareFormHeadJnDao.pageWhere(TableConstants.T_DECLARE_FORM_HEAD_JN, head,
                request.getStart(), request.getEnd(), request.getSortOrder(), "AND RELATE_DECL_CUST_ID IS NULL");

        GridResult result = new GridResult(orderHeadList, request, count);
        return result;
    }

    /**
     * 查询已关联报关单的申报单列表
     *
     * @param head
     * @param request
     * @return
     */
    @RequestMapping("/queryRelateCustDeclareList")
    @ResponseBody
    public GridResult queryRelateCustDeclareList(TDeclareFormHeadJn head, GridRequest request) {

        int count = declareFormHeadJnDao.count(TableConstants.T_DECLARE_FORM_HEAD_JN, head);
        List<TDeclareFormHeadJn> orderHeadList = declareFormHeadJnDao.page(TableConstants.T_DECLARE_FORM_HEAD_JN, head,
                request.getStart(), request.getEnd(), request.getSortOrder());

        GridResult result = new GridResult(orderHeadList, request, count);
        return result;
    }

    @RequestMapping("/detail")
    public String detail(TDeclareFormHeadJn head, ModelMap modelMap) {
        head = declareFormHeadJnDao.query(TableConstants.T_DECLARE_FORM_HEAD_JN, head);

        modelMap.put("head", head);

        String isCross = head.getIscross();
        String ieFlag = head.getImSign();
        String peflag = head.getPeflag();

        if("I".equals(ieFlag))
        {
            throw new RuntimeException("只支持跨境出口邮快件申报单查看");
        }

        //跨境 邮件
        if("1".equals(isCross) && "P".equals(peflag))
        {
            return "/togatherDeclareMgr/custDeclarationGenerate/detail_crossExportPost";
        }
        //跨境 快件
        if("1".equals(isCross) && "E".equals(peflag))
        {
            return "/togatherDeclareMgr/custDeclarationGenerate/detail_crossExportExpress";
        }

        throw new RuntimeException("只支持跨境出口邮快件申报单查看");
    }


    @RequestMapping("/pageGoods")
    @ResponseBody
    public GridResult pageGoods(TDeclareFormItemJn item, GridRequest request) {

        int count = declareFormItemJnDao.count(TableConstants.T_DECLARE_FORM_ITEM_JN, item);
        List<TDeclareFormItemJn> itemList = declareFormItemJnDao.page(TableConstants.T_DECLARE_FORM_ITEM_JN, item,
                request.getStart(), request.getEnd(), request.getSortOrder());

        GridResult result = new GridResult(itemList, request, count);
        return result;
    }

    /**
     * 申报单申报操作
     *
     * @param idList
     *
     * @return
     */
    @RequestMapping("/declare")
    @ResponseBody
    public Result declare(@RequestParam("id") List<String> idList) {
        Result result = new Result();
        try
        {
            custDeclarationGenerateService.toDeclareCust(idList);
        }
        catch(Exception e)
        {
            String msg = e.getMessage()+"";
            LOG.error(msg, e);

            result.setCode("0");
            result.setMsg(msg);
        }

        return result;
    }
}
